<?php 
	//---  获得post数据  ---
	$friendID = $_REQUEST['FriendID'];
	$userID = $_REQUEST['UserID'];
	$UUID = $_REQUEST['UUID'];
	//－－－  连接服务器  －－－
	$con = mysql_connect("127.0.0.1:3306","root","1qazse4");
	$DataName = 'ShareNavigation';
	if(!$con)
	{
		die('无法连接远程服务器: '.mysql_error);
	}
	//－－－  链接指定数据库  －－－
	mysql_query("SET NAMES UTF8");
	mysql_select_db($DataName,$con);
	
	
	$query = "SELECT * FROM Friend 
	WHERE UserID = \"$friendID\" && FriendID = \"$userID\" && IsAccept = 0";
	$result = mysql_query($query);
	if(mysql_num_rows($result) > 0)
	{
		$query = "INSERT  INTO Friend 
		(UUID,UserID,FriendID,IsAccept) 
		VALUES (\"$UUID\",\"$userID\",\"$friendID\",1)";
		$query1 = "UPDATE Friend SET IsAccept = 1 
		WHERE UserID = \"$friendID\" && FriendID = \"$userID\"";
		$result = mysql_query($query);
		$result1 = mysql_query($query1);
		if($result && $result1)
		{
			die ('TRUE'); 
		}
		else
		{
			
			die ('FALSE'.mysql_error());
		} 
	}
	
	else
   {
		$query = "SELECT * FROM Friend 
		WHERE UserID = \"$userID\" && FriendID = \"$friendID\"";
		$result = mysql_query($query);
		//－－－  如果有结果，则没有申请为好友过  －－－
		if(mysql_num_rows($result) < 1)
		{
			$query = "INSERT  INTO Friend 
			(UUID,UserID,FriendID,IsAccept) 
			VALUES (\"$UUID\",\"$userID\",\"$friendID\",0)";
			$result = mysql_query($query);
			if(!$result)
			{
				//---  查询出错   ----
				die('FALSE');
			}
			else 
			{
				echo 'TRUE';
			}
		}
		else 
		{
			$row = mysql_fetch_array($result);
			//---  如果返回结果为2,则为被拒绝过，就重新设置为0,请求添加状态   ---
			if($row['IsAccept'] == 2)
			{
				$query = "UPDATE Friend SET IsAccept = 0 
				WHERE UserID = \"$userID\" && FriendID = \"$friendID\"";
				if(!$result = mysql_query($query))
				{
					echo 'FALSE';
				}
				else
				{
					echo 'TRUE';
				}
			}
			else
			{
				echo 'TRUE';
			}
		}//end if(mysql_num_rows($result) < 1)  end eles
	}
	
	
	
	mysql_close($con);
?>